home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / Macintosh Sample Code / SC.023.FracApp 2.0 / UAreaSelector.inc1.p < prev    next >
Encoding:
Text File  |  1990-04-30  |  6.1 KB  |  182 lines  |  [TEXT/MPS ]

  1. {[j=20/53/1$]}
  2.  
  3. {-------------------------------------------------------------------------------------------}
  4. {$S ASelCommand}
  5.  
  6. PROCEDURE TAreaSelector.IAreaSelector(itsView: TFracAppView; itsDocument: TFracAppDocument);
  7.  
  8. { Initialize the selector object itself by calling ICommand. Also set the boolean that says
  9.   we constrain the mouse, so that our TrackConstrain method gets called. }
  10.  
  11.     BEGIN
  12.         { initialize normal parts of command }
  13.         SELF.INoChangesCommand(cMouseCommand, itsDocument, itsView, NIL);
  14.  
  15.         fConstrainsMouse := TRUE;                    { do the constrain to match to screen. }
  16.  
  17.         fOwnerView := itsView;                        { save the view for use in tracking. }
  18.         fPlotWidth := itsDocument.GetPlotWidth;
  19.         fPlotHeight := itsDocument.GetPlotHeight;
  20.  
  21.     END;                                            { TAreaSelector.IAreaSelector }
  22.  
  23. {-------------------------------------------------------------------------------------------}
  24. {$S ADoCommand}
  25.  
  26. FUNCTION TAreaSelector.TrackMouse(aTrackPhase: TrackPhase; VAR anchorPoint, previousPoint,
  27.                                   nextPoint: VPoint;
  28.     mouseDidMove: Boolean): TCommand; OVERRIDE;
  29.  
  30. { Track the mouse while the button is down. This is overridden so we can make sure that the
  31.   selection is cleared when we start tracking, and properly set when we are done tracking. }
  32.  
  33.     VAR
  34.         selPatHandle:        PatHandle;
  35.         tempRect:            Rect;
  36.  
  37.     BEGIN
  38.         TrackMouse := SELF;                         { Make sure we return something valid. }
  39.  
  40.         CASE aTrackPhase OF
  41.             trackPress: BEGIN
  42.                 fOwnerView.SetSelection(gZeroRect, kRedraw); { clear rect, there isn’t one. }
  43.             END;
  44.  
  45.             trackRelease: BEGIN
  46.                 Pt2Rect(VPtToPt(anchorPoint), VPtToPt(nextPoint), tempRect);
  47.                 fOwnerView.SetSelection(tempRect, kRedraw);
  48.             END;
  49.         END;                                        { Case on aTrackPhase }
  50.     END;                                            { TAreaSelector.TrackMouse }
  51.  
  52. {-------------------------------------------------------------------------------------------}
  53. {$S ADoCommand}
  54.  
  55. PROCEDURE TAreaSelector.TrackFeedback(anchorPoint, nextPoint: VPoint; turnItOn,
  56.                                       mouseDidMove: Boolean); OVERRIDE;
  57.  
  58. { Track the mouse giving the feedback of a different rectangle kind.  This is so we can use
  59.   the selection pattern to give a preferred rectangle.    The selection pattern comes out of
  60.   temporary memory so as to not fail needlessly. }
  61.  
  62.     VAR
  63.         selBoy:             Rect;
  64.         selPatHandle:        PatHandle;
  65.  
  66.     BEGIN
  67.         IF mouseDidMove THEN BEGIN                    {the pen is already in patXOR mode,
  68.                                                      black, one wide}
  69.             selPatHandle := GetPattern(kSelPattern); { get the pattern we use. }
  70.             IF selPatHandle <> NIL THEN             { use our pattern if available. }
  71.                 PenPat(selPatHandle^^);             { set pen pattern to our selection kind.
  72.                                                      }
  73.  
  74.             Pt2Rect(VPtToPt(anchorPoint), VPtToPt(nextPoint), selBoy);
  75.             FrameRect(selBoy);
  76.         END;
  77.     END;                                            { TAreaSelector.TrackFeedback }
  78.  
  79. {-------------------------------------------------------------------------------------------}
  80. {$S ADoCommand}
  81.  
  82. PROCEDURE TAreaSelector.TrackConstrain(anchorPoint, previousPoint: VPoint;
  83.                                        VAR nextPoint: VPoint); OVERRIDE;
  84.  
  85. { Constrain the mouse to a rectangle that is the same proportion as the document, so we can
  86.   make the selection match better without having to guess at the length or width, or scaling
  87.   the chosen rect to fit the document.    Small piece chosen will blow up to fit easily.    This
  88.   will make it easier to choose a selection that gives a 1:1 aspect ratio.    This also
  89.   chooses which direction the mouse has moved, deciding which is larger in order to decide
  90.   the direction to constrain. }
  91.  
  92.     VAR
  93.         newWidth, newHeight: LongInt;
  94.         mouseRatio, plotRatio: Real;
  95.  
  96.     PROCEDURE ChangeWidth;
  97.  
  98.         BEGIN
  99.             { Get the new width as a positive number, a displacement that is constrained. }
  100.  
  101.             newWidth := ABS(nextPoint.v - anchorPoint.v) * fPlotWidth DIV fPlotHeight;
  102.  
  103.             { Decide which quadrant we are in, moving the right direction. }
  104.  
  105.             IF nextPoint.h < anchorPoint.h THEN
  106.                 newWidth := - newWidth;
  107.  
  108.             { Actually change the final point to pass back. }
  109.  
  110.             nextPoint.h := anchorPoint.h + newWidth; { add offset to get new pt. }
  111.         END;
  112.  
  113.     PROCEDURE ChangeHeight;
  114.  
  115.         BEGIN
  116.             { Get the new height as a positive number, a displacement that is constrained. }
  117.  
  118.             newHeight := ABS(nextPoint.h - anchorPoint.h) * fPlotHeight DIV fPlotWidth;
  119.  
  120.             { Decide which quadrant we are in, moving the right direction. }
  121.  
  122.             IF nextPoint.v < anchorPoint.v THEN
  123.                 newHeight := - newHeight;
  124.  
  125.             { Actually change the final point to pass back. }
  126.  
  127.             nextPoint.v := anchorPoint.v + newHeight; { add offset to get new pt. }
  128.         END;
  129.  
  130.     PROCEDURE PinPoint;
  131.     { Pin the rectangle to the edge of the document. }
  132.  
  133.         VAR
  134.             constrainRect:        VRect;
  135.  
  136.         BEGIN
  137.             {$Push} {$H-}
  138.             SetVRect(constrainRect, 0, 0, fPlotWidth, fPlotHeight);
  139.             {$Pop}
  140.             PinVRect(constrainRect, nextPoint);
  141.         END;
  142.  
  143.     BEGIN
  144.         mouseRatio := ABS((nextPoint.h - anchorPoint.h) / (nextPoint.v - anchorPoint.v));
  145.         plotRatio := fPlotWidth / fPlotHeight;
  146.  
  147.     { The deltaX, deltaY can be thought of as a rect too. If the ratio of sides on
  148.       that rect (width/height) is greater than the ratio of width/height of the
  149.       plot rectangle, then we need to grow the height of the rect. If it is less,
  150.       we need to grow the width. This is a ratio of sides to decide which way to
  151.       grow. We grow to make the new rect still touch the mouse position. It can be
  152.       thought of as the rectangle being thicker than tall wanting to grow the tall
  153.       part in a constrained way, and the corollary for the width. }
  154.  
  155.         IF mouseRatio > plotRatio THEN BEGIN        { constrain height to new value. }
  156.             ChangeHeight;
  157.             PinPoint;
  158.             ChangeWidth;
  159.         END
  160.         ELSE BEGIN                                    { constrain width to new value. }
  161.             ChangeWidth;
  162.             PinPoint;
  163.             ChangeHeight;
  164.         END;
  165.     END;                                            { TAreaSelector.TrackConstrain }
  166.  
  167. {-------------------------------------------------------------------------------------------}
  168. {$S AFields}
  169.  
  170. PROCEDURE TAreaSelector.Fields(PROCEDURE DoToField(fieldName: Str255; fieldAddr: Ptr;
  171.                                                    fieldType: Integer)); OVERRIDE;
  172.  
  173.     BEGIN
  174.         DoToField('TFastFracAppEngine', NIL, bClass);
  175.         DoToField('fOwnerView', @fOwnerView, bObject);
  176.         DoToField('fPlotHeight', @fPlotHeight, bObject);
  177.         DoToField('fPlotWidth', @fPlotWidth, bObject);
  178.  
  179.         { Print fields of anscestors }
  180.         INHERITED Fields(DoToField);
  181.     END;
  182.